import store from '../store/global.js'
const whiteList = [
    "/pages/login/login",
    "/pages/login/reg",
    "/pages/login/forget",
	"/pages/login/agreement",
    "/pages/index/index",
    "/pages/recommend/index",
    "/pages/cart/index",
    "/pages/mine/mine",
    "/pages/active/index",
	"/pages/product/index",
	"/pages/notice/notice",
	"/pages/notice/notice_detail",
	"/pages/search/search",
	"/pages/search/search_child",
	"/pages/recommend/category_list",
	"/pages/rural_zone/rural_zone",
	"/pages/birthday_zone/birthday_zone",
	"/pages/home_sub/home_sub",
	"/pages/home_sub/fc_active",
	"/pages/home_sub/film_active",
	"/pages/home_sub/union_active"
	
];

function hasPermission(url){
    let currentUrl = url;
    if (url.includes("?")) {
        currentUrl = url.split("?")[0];
    }
    // 在白名单中或有token，直接跳转
    if (whiteList.indexOf(currentUrl) !== -1 || uni.getStorageSync("token") || store.state.token) {
		return true;
    }
    return false;
}

const jumpInterceptorList = ["navigateTo", "redirectTo", "reLaunch", "switchTab"];


//应用权限校验至拦截器
jumpInterceptorList.forEach((item) => {
    uni.addInterceptor(item, {
        //拦截前触发
        invoke(args) {
			///hasPermission 为true 不验证登录     false 跳转登录
            if (!hasPermission(args.url)) {
				// console.log('hh2:'+args.url);
                uni.setStorageSync("URL", args.url);
                uni.navigateTo({
                    url: "/pages/login/login"
                });
                return false;
            }
            return true;
        },
        fail(e) {
            console.error(`为 ${item} 拦截器添加权限校验失败：`, e);
        }
    });
});
